﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace IMark.DAO
{
    public class NguoiDungModels
    {
        public static List<NGUOI_DUNG> GetAll()
        {
            using(var context = new EMartEntities())
            {
                return context.NGUOI_DUNG.ToList();
            }
        }

        public static NGUOI_DUNG GetByID(long ID)
        {
            using(var context = new EMartEntities())
            {
                return context.NGUOI_DUNG.Find(ID);
            }
        }

        public static bool Add(NGUOI_DUNG nguoiDung)
        {
            using(var context = new EMartEntities())
            {
                try
                {
                    context.NGUOI_DUNG.Add(nguoiDung);
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static bool Update(NGUOI_DUNG nguoiDung)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    NGUOI_DUNG existing = context.NGUOI_DUNG.Find(nguoiDung.ID);
                    existing.DiaChi = nguoiDung.DiaChi;
                    existing.DienThoai = nguoiDung.DienThoai;
                    existing.Email = nguoiDung.Email;
                    existing.GioiTinh = nguoiDung.GioiTinh;
                    existing.HoTen = nguoiDung.HoTen;
                    existing.IDLoaiNguoiDung = nguoiDung.IDLoaiNguoiDung;
                    existing.MatKhau = nguoiDung.MatKhau;
                    existing.NgaySinh = nguoiDung.NgaySinh;
                    existing.TenDangNhap = nguoiDung.TenDangNhap;                    
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }
            }
        }

        public static bool CheckUserName(string username)
        {
            using (var context = new EMartEntities())
            {
                var result = context.NGUOI_DUNG.Where(u => u.TenDangNhap.Equals(username));
                if (result != null && result.Count() > 0)
                    return true;
            }
            return false;
        }

        public static bool CheckEmail(string email)
        {
            using (var context = new EMartEntities())
            {
                var result = context.NGUOI_DUNG.Where(u => u.Email.Equals(email));
                if (result != null && result.Count() > 0)
                    return true;
            }
            return false;
        }

        public static bool ChangePassword(int ID, string newPassword)
        {
            using (var context = new EMartEntities())
            {
                try
                {
                    NGUOI_DUNG existing = context.NGUOI_DUNG.Find(ID);
                    existing.MatKhau = newPassword;
                    context.SaveChanges();
                    return true;
                }
                catch (Exception)
                {
                    return false;
                }

            }
        }        
        /// <param name="username">Tên đăng nhập</param>
        /// <param name="password">Password đã mã hóa MD5</param>        
        public static NGUOI_DUNG Login(string username, string password)
        {
            NGUOI_DUNG nguoiDung = null;
            using (var context = new EMartEntities())
            {
                var result = context.NGUOI_DUNG.Where(p => p.TenDangNhap.Equals(username) &&
                    p.MatKhau.Equals(password));
                if (result != null && result.Count() > 0)
                {
                    nguoiDung = result.FirstOrDefault();
                }
            }
            return nguoiDung;
        }






    }
}